package cn.edu.xmut.chen.zhlg.zhlg.user.controller;

import cn.edu.xmut.chen.zhlg.common.HttpResult.Result;
import cn.edu.xmut.chen.zhlg.zhlg.user.entity.Resume;
import cn.edu.xmut.chen.zhlg.zhlg.user.param.ResumePageSearchParam;
import cn.edu.xmut.chen.zhlg.zhlg.user.result.ResumeResult;
import cn.edu.xmut.chen.zhlg.zhlg.user.service.ResumeService;
import cn.edu.xmut.chen.zhlg.zhlg.user.service.impl.UserServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

@RestController
@RequestMapping("/resume")
@CrossOrigin
public class ResumeController {
    private static final Logger log = LoggerFactory.getLogger(ResumeController.class);
    @Autowired
    private ResumeService resumeService;
    @Autowired
    private UserServiceImpl userServiceImpl;

    @PostMapping("/save")
    public boolean saveOrUpdate(@RequestBody Resume resume) {
        QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", resume.getUserId());
        return resumeService.saveOrUpdate(resume,queryWrapper);
    }

    @GetMapping("/detail")
    public ResumeResult getDetail(String userId) {
        ResumeResult  resumeResult = new ResumeResult();
        BeanUtils.copyProperties(resumeService.getOne(new QueryWrapper<Resume>().eq("user_id", userId)), resumeResult);
        String phone = userServiceImpl.getById(userId).getPhone();
        resumeResult.setPhone(phone);
        log.info("用户：{}", resumeResult);
        return resumeResult;
    }

    @GetMapping("/personal")
    public ResumeResult getPersonal(String userId) {
        ResumeResult resumeResult = new ResumeResult();
        Resume resume = resumeService.getOne(new QueryWrapper<Resume>().eq("user_id", userId));
        if (resume != null) {
            BeanUtils.copyProperties(resume, resumeResult);
            resumeResult.setAviator(userServiceImpl.getById(resume.getUserId()).getPic());
        }
        return resumeResult;
    }

    @PostMapping("/upload")
    public String uploadFile(@RequestParam Long userId, @RequestParam MultipartFile file) {
        return "uploaded_url/" + file.getOriginalFilename();
    }

    @DeleteMapping("/delete")
    public boolean delete(@RequestParam Long id) {
        return resumeService.removeById(id);
    }

    @GetMapping("/list")
    public List<ResumeResult> list() {
        List<Resume> resumeList = resumeService.list();
        List<ResumeResult> resumeResultList;

        resumeResultList = resumeList.stream().map(resume -> {
            ResumeResult resumeResult = new ResumeResult();
            BeanUtils.copyProperties(resume, resumeResult);
            resumeResult.setAviator(userServiceImpl.getById(resume.getUserId()).getPic());
            return resumeResult;
        }).toList();
        return resumeResultList;
    }

    @GetMapping("/pageSearch")
    public Result pageSearch(ResumePageSearchParam param) {
        return Result.success("获取成功").data(resumeService.pageSearch(param));
    }
} 